package com.tsy.common.utils.converter;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.InputStream;

@Slf4j
public class PdalUtil {

    // 定义 PDAL 工具的路径，根据不同系统进行设置
    private static final String CONVERTER_WINDOWS = "PDAL\\bin\\pdal.exe";
    private static final String CONVERTER_LINUX = "pdal";
    // 根据当前系统选择合适的工具路径
    private static final String CONVERTER_TOOL = System.getProperty("os.name").startsWith("Windows") ? CONVERTER_WINDOWS : CONVERTER_LINUX;

    // 添加带进度回调的方法
    public static boolean pintCloudTypeTranslate(String srcPath, String destPath) {
        File file = new File(destPath);
        File parent = file.getParentFile();
        // 如果保存路径不存在，则创建路径
        if (!parent.exists()) {
            parent.mkdirs();
        }
        StringBuilder cmd = new StringBuilder();
        cmd.append(CONVERTER_TOOL);
        cmd.append(" ");
        cmd.append("translate ");
        cmd.append(srcPath);
        cmd.append(" ");
        cmd.append(destPath);
        System.out.println("执行命令: " + cmd);

        boolean result = true;
        try {
            Process proc = Runtime.getRuntime().exec(cmd.toString());
            PdalThread error = new PdalThread(proc.getErrorStream());
            PdalThread output = new PdalThread(proc.getInputStream());
            error.start();
            output.start();
            proc.waitFor();
        } catch (Exception e) {
            result = false;
            log.error("转换失败: " + e.getMessage());
        }
        return result;
    }

    // 自定义线程类，用于处理进程的输入流和错误流
    static class PdalThread extends Thread {
        private InputStream is;

        public PdalThread(InputStream is) {
            this.is = is;
        }

        @Override
        public void run() {
            try {
                int c;
                while ((c = is.read()) != -1) {
                    // 这里可以根据需要处理输入流的内容
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }



    public static void main(String[] args) {
        System.out.println("开始转换测试...");
        boolean b = PdalUtil.pintCloudTypeTranslate("C:\\pointCloud\\test\\L5-1-M01-001.ply", "C:\\pointCloud\\test\\test.las");

        System.out.println("转换测试完成");
    }

}

